Skip to content

Add immutable read_header API - #763

Open
tompollard wants to merge 5 commits into
mainfrom
typed-read-header
Open

tompollard wants to merge 5 commits into
mainfrom
typed-read-header

Conversation

@tompollard

@tompollard tompollard commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Add wfdb.read_header(path) as a metadata-only API. It returns an immutable SingleSegmentHeader with per-signal entries or a MultiSegmentHeader with segment references. The new objects use clearer names such as n_signals, n_frames, samples_per_frame, and format, while keeping WFDB terms such as fs, adc_gain, baseline, and skew.

The result describes one physical header. It applies some WFDB header defaults but leaves omitted optional fields such as adc_zero as None, and keeps n_frames as None when length is omitted. It reads no signal samples or referenced segment headers. For a local .wfdb archive, segment= selects one header inside the archive.

rdheader() continues to return mutable Record/MultiRecord objects, including its rd_segments=True behavior. This makes read_header() a deliberate new API rather than a direct alias, as discussed in #743 and #672. I have not added a warning to rdheader() because its recursive behavior does not yet have an equivalent in the new API.

The new API retains inline annotations. This PR does not ship py.typed: package-wide typing needs further work on the existing public APIs before we ask type checkers to rely on them.

Checks

  • 10 focused read_header tests passed.
  • Sphinx HTML build with warnings as errors passed.
  • Ruff format and lint passed.
  • uv build passed; the wheel contains the new API and no py.typed marker.

@HeywoodGeblomi

Copy link
Copy Markdown
Contributor

Thanks for putting this together, Tom. I tried wfdb.read_header from this branch on a real record where baseline and adc_zero differ: nstdb 118e24, the case from #752. It reports baseline=1024, adc_zero=0 for both signals, which matches the header line 212 200(1024) 11 0 ... exactly. On mitdb 118, where the header omits the baseline, it reports baseline=1024, adc_zero=1024, which matches the spec's "baseline defaults to ADC zero" rule. Both records gave the same values through rdheader.

One small question, with no change implied: on a minimal header (t.dat 16), baseline comes back as 0 but adc_zero is None, and the spec default for adc_zero is 0. Is leaving adc_zero, adc_res and init_value as None when omitted intentional, as in "not stated", rather than filled in like baseline?

Thanks again, and no rush.

Heywood Geblomi

tompollard added a commit that referenced this pull request Nov 28, 2026
## Summary

Prepare a 5.6.0 minor release from current `main` as a checkpoint for
the merged record I/O refactoring. The release keeps the existing public
API and leaves the proposed `read_header()` API in #763 for a later
release.

This bumps the version and collects the pending changelog fragments for
the record comparison and `csv2ann` fixes, the calibration
documentation, and the internal reader changes.

## Checks

- `uv build`
- Ruff format and lint checks
- Sphinx HTML build with warnings treated as errors
- `git diff --check`

The built wheel reports version 5.6.0 and does not contain the new
`read_header()` module from #763.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a runtime regression, but I would resolve two parts of the public API before release:

  1. read_header is described as typed, but its public signature has no parameter or return annotations (src/wfdb/io/record.py:2292), and the package has no py.typed marker. Installed-package type checkers therefore cannot expose the return type to callers. If static typing is intended, please annotate the signature and decide how to publish the type information. Otherwise, describe this as an immutable, structured header rather than a typed API.

  2. SegmentHeader (src/wfdb/io/headers.py:34-39) contains only a segment name and length. It does not contain the referenced segment's header. I would call this SegmentReference or SegmentEntry before the name becomes public; changing it later would break callers.

I would also confirm the intended multi-segment contract. For 041s, read_header() reports seven signals but exposes none of their names or units; rdheader(..., rd_segments=True) can retrieve them. Reading exactly one physical header per call is a coherent design, but the documentation should make clear how callers obtain segment signal metadata. In particular, a typed layout-only read would address the use case in #442. If read_header is intended to replace rdheader, I would decide on that access pattern before promoting it.

The full offline suite passed (934 passed, 4 skipped, 21 deselected). I also compared the shared fields against rdheader() across all 88 sample headers and found no differences.

@tompollard

tompollard commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

@HeywoodGeblomi Yes, that is intentional. read_header follows rdheader here: an omitted adc_zero, adc_res, or init_value remains None, so callers can tell that the field was not stated in the header. The effective WFDB default for adc_zero is still 0. baseline is the exception: the parser resolves it to the stated adc_zero, or to 0 when neither is stated, because it is used for signal calibration.

I clarified this distinction in the SignalHeader docstring and I/O docs, and added a minimal-header test in 997cf00. Thanks for checking the nstdb and mitdb examples as well.

@tompollard

tompollard commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

I addressed the public API review and rebased the PR onto 5.6.0.

  1. read_header has annotated parameters and a SingleSegmentHeader | MultiSegmentHeader return type. I initially added py.typed, then removed it in 09d87ca after feedback showed that it exposes type-checking errors in existing public APIs. The inline annotations remain; package-wide type support can follow once those APIs are ready.
  2. Renamed SegmentHeader to SegmentReference in the public exports, docs, and tests. It holds the name and frame count from a segment line; it does not imply that the referenced header has been loaded.
  3. The contract is one physical header per read_header call. The docs show how to read a referenced segment to get signal metadata. A zero-frame layout reference can be read directly for Reading MS signal metadata in rdheader #442, and a ~ gap has no header. rdheader(..., rd_segments=True) remains the recursive option.

The focused tests, Ruff, docs build, and package build passed after the latest change.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update addresses my previous points: the public function has type annotations, the wheel includes py.typed, SegmentReference names the entry accurately, and the one-header-per-call behavior is documented.

One case remains for multi-segment .wfdb archives. read_header(archive_path) reads only the main header (src/wfdb/io/record.py:2404-2412), and the public API has no way to select a segment header inside that archive. I reproduced this with an archive containing 041s.hea, 041s01.hea, and 041s02.hea: rdheader(archive_path, rd_segments=True) retrieves the first segment's signal names, but passing archive_path/041s01 to read_header raises NotADirectoryError. The documentation's advice to pass a segment path to read_header does not work for this supported archive form.

I would decide before release whether to add a way to name an archive member, such as an optional segment argument, or explicitly document that typed segment reads from archives are unavailable. An archive member option would preserve the one-physical-header-per-call contract.

The full offline suite passed (935 passed, 4 skipped, 21 deselected).

@tompollard

Copy link
Copy Markdown
Member Author

Good catch. I added a segment argument in 380f157, so a caller can read a referenced header inside the same local archive:

main = wfdb.read_header("041s.wfdb")
first = wfdb.read_header("041s.wfdb", segment=main.segments[0].name)

This still returns one physical header per call. The argument also works for a variable-layout record's zero-frame layout segment. I updated the docs and added tests for fixed-layout and variable-layout archives, missing members, and invalid argument combinations. The offline suite passed (937 passed, 4 skipped, 21 network tests deselected), as did Ruff and the warnings-as-errors docs build.

@tompollard

tompollard commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

Follow-up to my archive reply: I considered making segment= work for ordinary record paths and memory URIs too, but decided against it and removed that change. For an ordinary record, the segment already has a direct path, such as wfdb.read_header("path/to/041s01"). A read_header(parent, segment=name) call would only use the parent's directory without checking the parent header; it could read an unrelated sibling even if the named parent does not exist. An archive member has no separate path, so segment= remains specific to local .wfdb archives.

@tompollard tompollard changed the title Add typed read_header API Add immutable read_header API Nov 28, 2026
@tompollard

Copy link
Copy Markdown
Member Author

I removed py.typed in 09d87ca after the type-checker feedback. Shipping that marker now would expose typing problems in existing APIs to users who have not changed their code. read_header and the new dataclasses keep their inline annotations, but the PR now describes the API as immutable and structured rather than promising package-wide type support.

The rebuilt wheel has no py.typed marker. The 10 focused tests, Ruff checks, and warnings-as-errors docs build passed. I also updated the PR description and corrected my earlier comment about the marker.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants